//app.js
const app = getApp(); 
App({
  onLaunch: async function () {
    var that = this 
    // 登录
    //const res = await wx.login();
   // console.log('res.code', res.code);
       // 登录
       wx.login( 
        { 
         success: function (res) { 
           console.log("code"+res.code);
           if (res.code) { 
             wx.request({ 
               url: 'https://pnst.tsintell.com/portal/user/getOpenId',
               //url: 'http://localhost:8066/portal/user/getOpenId',  
               //url: 'http://124.71.238.22:8099/portal/user/getOpenId',  
               data: { 
                 code : res.code,
                 sysUserId:2 
               }, 
               method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT   
               header: { 
                 'Content-Type': 'application/x-www-form-urlencoded' 
               }, // 设置请求的 header   
               success: function (res) { 
                var obj = {}; 
                obj.openid = res.data.data; 
                console.log("openid=="+res.data.data);
                //存储openid 
                wx.setStorageSync('openid', obj.openid);  
                              //  //微信登陆 
                              that.weChatLogin(res.data.data); 
                // portal.mineUseInfo(obj);
  
               } 
             }); 
           } else { 
             console.log('获取用户登录态失败！' + res.errMsg) 
           } 
         } 
       } 
   ); 
  },
  weChatLogin(openId){ 
    var that =this; 
    //微信登陆 
    wx.request({ 
      url: 'https://pnst.tsintell.com/portal/loginWechatInfo',  
      //url: 'http://localhost:8066/portal/loginWechatInfo', 
      //url: 'http://124.71.238.22:8099/portal/loginWechatInfo',   
      method: 'POST', 
      data:{ 
        openId :openId
      }, 
      header: {//定死的格式，不用改，照敲就好 
        'Content-Type': 'application/x-www-form-urlencoded', 
      }, 
      success: function (res) {//这里写调用接口成功之后所运行的函数 
        var code = res.data.code; 
        if(code == 0){ 
        wx.setStorageSync("sessionId", res.header["Set-Cookie"]);//返回的cookie 
        //app.phone = res.data.user.phone;
        console.log("缓存手机号=="+res.data.user.phone)
          wx.setStorageSync('phone', res.data.user.phone) 
         
      //  that.getIdentity(openId); 
      
        wx.switchTab({ 
          url: '/pages/mine/mine', 
        }); 
 
        }else{ 
       
        } 
      }, 
      fail: function (res) {//这里写调用接口失败之后所运行的函数 
        console.log('.........fail..........'); 
        console.log(res) 
      } 
    }) 
  }, 
  onHide: function () {
    this.globalData.isMerchant = false;
    this.globalData.isPromote = false;
  },
  globalData: {
    userInfo: null
  }
})